package com.example.demo.receiver;


import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

/**
 * Created by crj on 2018/1/22.
 */
@Component
public class Receiver {

    private Logger log = LoggerFactory.getLogger(Receiver.class);

    private final String QUEUE_NAME_TEST = "test";
    private final String QUEUE_NAME_CHINA = "china";

    /***
     * 简单队列
     * @param str
     * @throws Exception
     */
    @RabbitListener(queues = QUEUE_NAME_TEST)
    public void receiver(String str, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {
        try {
        log.info("Recever:  " + str + "  " + "from queue:    " + QUEUE_NAME_TEST);
        //制造异常
        // int i =1/(1-1);
        //收到模式ack -告诉mq,已经收到消息(一切正常 MQ可以进行删除)
        channel.basicAck(tag, false);
        } catch (Exception e) {
            log.error(e.getMessage());
            //重回队列
            channel.basicNack(tag, false, true);
            //拒绝消息
           // channel.basicReject(tag, true);
        }

    }

    @RabbitListener(queues = QUEUE_NAME_CHINA)
    public void cahinaRecerver(String str) throws Exception {
        log.info("Recerver: " + "from queue:    " + QUEUE_NAME_CHINA);
//        throw  new Exception("dsadddddd");

    }


}
